import { getCourseList, getCourseDetail, getMyCourseList } from '../services/course'

export default {

  namespace: 'course',

  state: {
    courseList: [],
    detail: {},
    myCourseList: []
  },

  subscriptions: {
    setup({ dispatch, history }) {
    },
  },

  effects: {
    *fetch({ payload }, { call, put }) {
      const res = yield call(getCourseList, payload);
      if (res.data.code == 0) {
        yield put({ type: 'save', payload: res.data.data });
      }
    },
    *fetchMy({ payload }, { call, put }) {
      const res = yield call(getMyCourseList, payload);
      if (res.data.code == 0) {
        yield put({ type: 'saveMy', payload: res.data.data || [] });
      }
    },
    *fetchDetail({ payload }, { call, put }) {
      const res = yield call(getCourseDetail, payload);
      if (res.data.code == 0) {
        yield put({ type: 'saveDetail', payload: res.data.data });
      }
    }
  },

  reducers: {
    save(state, action) {
      return { ...state, courseList: action.payload };
    },
    saveMy(state, action) {
      return { ...state, myCourseList: action.payload };
    },
    saveDetail(state, action) {
      return { ...state, detail: action.payload };
    }
  },

};
